package com.ljy.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.ljy.pojo.App_category;
import com.ljy.pojo.App_info;
import com.ljy.pojo.Backend_user;
import com.ljy.pojo.Data_dictionary;
import com.ljy.service.BackendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/app")
public class Controoler {

    //controller 调用 service 层
    @Autowired
    @Qualifier("BackendServiceIMP")
    private BackendService service;

    /**
     * 后台管理系统
     *
     * @return
     */
    @RequestMapping("/login")
    public String backendlogin() {
        return "backendlogin";
    }

    /**
     * 退出至登录页面
     *
     * @param session
     * @return
     */
    @RequestMapping("/out")
    public String goOut(HttpSession session) {

        session.removeAttribute("userNames");
        session.removeAttribute("userCode");
        System.err.println("退出页面！");
        return "redirect:/app/Backendlogin";
    }


    /**
     * 后台登陆
     *
     * @return
     */
    @RequestMapping("/Backendlogin")
    public String Backendlogin(String userCode, String userPassword, Model model, HttpSession session) {

        System.out.println("userCode==>>" + userCode + ",userpassword===>>>" + userPassword);

        Backend_user user = service.Login(userCode, userPassword);

        System.err.println("Backend_user==>>" + user);
        if (user == null) {
            model.addAttribute("error", "登录失败！用户或密码错误！");
            return "backendlogin";
        }

        session.setAttribute("userNames", user.getUserName());
        session.setAttribute("userCode", user);

        return "backend/main";
    }


    /**
     * 主页面
     *
     * @return
     */
    @RequestMapping("/homepage")
    public String homepage() {
        return "backend/main";
    }


    /**
     * 点击进入app维护
     *
     * @return
     */
    @RequestMapping("/applist")
    public String applist(App_info appInfo, Model model, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) {


        //所属平台
        List<Data_dictionary> their = service.Their();
        System.err.println("所属平台=>>>>" + their);
        model.addAttribute("flatFormList", their);

        //一级分类
        List<App_category> classify1 = service.classify(0);
        System.err.println("一级分类" + classify1);
        model.addAttribute("categoryLevel1List", classify1);
        model.addAttribute("queryCategoryLevel1", appInfo.getCategoryLevel1());


        System.err.println("软件名：" + appInfo.getSoftwareName());
        System.err.println("APP状态：" + appInfo.getStatus());
        System.err.println("所属平台：" + appInfo.getFlatFormId());
        System.err.println("一级分类：" + appInfo.getCategoryLevel1());
        System.err.println("二级分类：" + appInfo.getCategoryLevel2());
        System.err.println("三级分类：" + appInfo.getCategoryLevel3());


        if (appInfo.getCategoryLevel2() != null) {
            //二级分类
            List<App_category> classify2 = service.classify(appInfo.getCategoryLevel1());
            System.err.println("二级分类" + classify2);
            model.addAttribute("categoryLevel2List", classify2);
            model.addAttribute("queryCategoryLevel2", appInfo.getCategoryLevel2());
        }

        if (appInfo.getCategoryLevel3() != null) {
            //三级分类
            List<App_category> classify3 = service.classify(appInfo.getCategoryLevel2());
            System.err.println("三级分类" + classify3);
            model.addAttribute("categoryLevel3List", classify3);
            model.addAttribute("queryCategoryLevel3", appInfo.getCategoryLevel3());
        }


        System.err.println("当前页数：" + pageNum);
        System.err.println("总条数：" + pageSize);


        //分页
        PageInfo<App_info> page = service.findPage(appInfo, pageNum, pageSize);
        System.err.println("查询所有app信息=====>>>" + page);

        model.addAttribute("page", page);

        model.addAttribute("appInfo", appInfo);

        return "backend/applist";
    }

    /**
     * 二级分类 以及第三级分类
     *
     * @param pid
     * @return
     */
    @RequestMapping("categorylevellist.json")
    @ResponseBody
    public String classify1(int pid) {

        System.err.println("从Ajax传过来的pid=>>>" + pid);

        List<App_category> classify = service.classify(pid);

        String queryCategoryLevel2 = JSON.toJSONString(classify);

        return queryCategoryLevel2;
    }

    /**
     * 审核
     *
     * @return
     */
    @RequestMapping("/check/{aid}/{vid}")
    public String check(@PathVariable("aid") Integer aid, @PathVariable("vid") Integer vid, Model model) {

        System.out.println("app编号：" + aid + ",vid编号：" + vid);


        Map appInfo = service.queryByAppinfo(aid);

        model.addAttribute("appInfo", appInfo);


        Map appVersions = service.queryByVersion(vid);

        System.out.println("根据传参过来的编号查询：" + appVersions);

        model.addAttribute("Versions", appVersions);

        return "backend/appcheck";
    }

    /**
     * 修改状态为审核
     *
     * @return
     */
    @RequestMapping("/checksave")
    public String checksave(Integer status, Integer id) {

        System.err.println("--------------------------->>>>>>>>>状态：" + status + ",编号：" + id);

        int updateByStatus = service.updateByStatus(status, id);


        //判断查询出来的APK是否为空
        boolean bo = ObjectUtils.isEmpty(updateByStatus);
        System.err.println("判断出来值为：" + updateByStatus);

        //判断没有数据
        if (!bo) {
            System.err.println("修改成功！");
        } else {
            System.err.println("修改失败！");

        }
        return "redirect:/app/applist";//重定向到@RequestMapping("/applist")请求;
    }


}
